/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.massfilerenamer.controller;

import br.com.barrientosjunior.massfilerenamer.command.RenameFilesCommand;
import br.com.barrientosjunior.massfilerenamer.parser.PatternParser;
import br.com.barrientosjunior.massfilerenamer.parser.element.Element;
import br.com.barrientosjunior.massfilerenamer.util.UserFileFilter;
import br.com.barrientosjunior.massfilerenamer.view.MainScreen;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * Controller for the MainScreen Swing interface.
 * @author Junior
 */
public class MainScreenController implements StatusController {

    private MainScreen mainScreen = null;

    public MainScreenController() {
        mainScreen = new MainScreen(this);
        mainScreen.pack();
        mainScreen.setVisible(true);
    }

    /**
     * Opens the dialog box where the user can choose a folder from which the
     * files will have their names changed.
     * @return
     */
    public String chooseFileOrFolder(){
        String folder = "";
        try {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setLocale(new Locale("pt", "BR"));
            fileChooser.setMultiSelectionEnabled(false);
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int result = fileChooser.showDialog(mainScreen, "Selecionar Pasta");

            if (result != JFileChooser.CANCEL_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                if (!selectedFile.isDirectory()){
                    throw new Exception("Somente pastas são aceitas. Uma adição para a próxima versão talvez?");
                    //Only folders are accepted. An addition for the next version maybe?
                } else {
                    folder = selectedFile.getCanonicalPath();
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(mainScreen,
                                          e.getMessage(),
                                          "Um Erro ocorreu",
                                          JOptionPane.ERROR_MESSAGE);
        }
        
        return folder;
    }

    /**
     * Create a preview of how the file's new names will be.
     * @param input folder from which the files names will be read.
     * @param pattern String containing the pattern.
     * @return String with the sample of a file name.
     */
    public String previewOutputFileName(String input, String pattern, String filter) {
        //only works with folders
        File file = new File(input);
        File[] files = file.listFiles(new UserFileFilter(filter));
        File file1 = files[0];
        String result = "";
        try {
            List<Element> formatedPattern = new PatternParser().parse(pattern);
            for (Iterator<Element> it = formatedPattern.iterator(); it.hasNext();) {
                Element element = it.next();
                element.setFile(file1);
                result = result + element.toString();
            }


        } catch (Exception e) {
            JOptionPane.showMessageDialog(mainScreen,
                                          e.getMessage(),
                                          "Um Erro ocorreu",
                                          JOptionPane.ERROR_MESSAGE);
        }

        return result;
        
    }

    public void setPercentStatus(int status) {
        this.mainScreen.getRenameJProgressBar().setValue(status);
    }

    public void renameFiles(String input, String output, String pattern, String filter){
        RenameFilesCommand command = new RenameFilesCommand(this, pattern, input, output, filter);
        command.execute();
    }

}
